package com.mall.business.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
        import com.mall.common.utils.DateUtils;
import com.mall.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mall.business.mapper.TUserAddressMapper;
import com.mall.business.domain.TUserAddress;
import com.mall.business.service.ITUserAddressService;
import org.springframework.util.CollectionUtils;

/**
 * 收货地址维护Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-30
 */
@Service
public class TUserAddressServiceImpl extends ServiceImpl<TUserAddressMapper,TUserAddress>  implements ITUserAddressService
{
    @Autowired
    private TUserAddressMapper tUserAddressMapper;

    /**
     * 查询收货地址维护
     *
     * @param id 收货地址维护主键
     * @return 收货地址维护
     */
    @Override
    public TUserAddress selectTUserAddressById(String id)
    {
        return tUserAddressMapper.selectTUserAddressById(id);
    }

    /**
     * 查询收货地址维护列表
     *
     * @param tUserAddress 收货地址维护
     * @return 收货地址维护
     */
    @Override
    public List<TUserAddress> selectTUserAddressList(TUserAddress tUserAddress)
    {
        return tUserAddressMapper.selectTUserAddressList(tUserAddress);
    }

    /**
     * 新增收货地址维护
     *
     * @param tUserAddress 收货地址维护
     * @return 结果
     */
    @Override
    public int insertTUserAddress(TUserAddress tUserAddress)
    {
                tUserAddress.setCreateTime(DateUtils.getNowDate());
            return tUserAddressMapper.insertTUserAddress(tUserAddress);
    }

    /**
     * 修改收货地址维护
     *
     * @param tUserAddress 收货地址维护
     * @return 结果
     */
    @Override
    public int updateTUserAddress(TUserAddress tUserAddress)
    {
                tUserAddress.setUpdateTime(DateUtils.getNowDate());
        return tUserAddressMapper.updateTUserAddress(tUserAddress);
    }

    /**
     * 批量删除收货地址维护
     *
     * @param ids 需要删除的收货地址维护主键
     * @return 结果
     */
    @Override
    public int deleteTUserAddressByIds(String[] ids)
    {
        return tUserAddressMapper.deleteTUserAddressByIds(ids);
    }

    /**
     * 删除收货地址维护信息
     *
     * @param id 收货地址维护主键
     * @return 结果
     */
    @Override
    public int deleteTUserAddressById(String id)
    {
        return tUserAddressMapper.deleteTUserAddressById(id);
    }


    @Override
    public TUserAddress getUserMrAddress() {
        TUserAddress address = new TUserAddress();
        address.setUserId(SecurityUtils.getUserIdStr());
        address.setIsMr("1");
        List<TUserAddress> tUserAddresses = this.selectTUserAddressList(address);
        if (!CollectionUtils.isEmpty(tUserAddresses)) {
            return tUserAddresses.get(0);
        } else {
            return null;
        }
    }

    @Override
    public   void  cancleUserOtherMrAddress() {
        this.update(new UpdateWrapper<TUserAddress>().set("is_mr","0").eq("user_id",SecurityUtils.getUserIdStr()).eq("is_mr","1"));
    }

}
